import xlwt  # 进行excel操作
import sqlite3  # 进行SQLite数据库操作
from selenium.webdriver.common.by import By
from selenium.webdriver import Chrome
import time
from selenium.webdriver.common.keys import Keys
import multiprocessing  # 进行多线程操作

datalist = []

# while True:
#     try:
web = Chrome()
time.sleep(5)
web.get("https://search.51job.com/")
time.sleep(5)
# web.find_element(By.XPATH, '//*[@id="keywordInput"]').send_keys('net工程师', Keys.ENTER)
# web.find_element(By.XPATH, '//*[@id="keywordInput"]').send_keys('web前端工程师', Keys.ENTER)
web.find_element(By.XPATH, '//*[@id="keywordInput"]').send_keys('Java工程师', Keys.ENTER)
time.sleep(1)
web.find_element(By.XPATH, '//*[@id="search_btn"]').click()
web.find_element(By.XPATH, '/html/body/div[2]/div[2]/div[4]/span').click()  # 把菜单栏展开
web.maximize_window()
time.sleep(5)
# 选择近一个月

web.find_element(By.XPATH, '/html/body/div[2]/div[2]/div[3]/div[5]/div[1]/div[2]/div[1]/div[3]/p').click()
web.find_element(By.XPATH, '/html/body/div[2]/div[2]/div[3]/div[5]/div[2]/p[5]/a').click()
time.sleep(2)
# 让页面刷新几秒钟
# time.sleep(5)

# 切换城市
web.find_element(By.XPATH, '/html/body/div[2]/div[2]/div[1]/div[1]/div[2]/div[2]').click()  # 点击全部城市
# 取消当前城市
web.find_element(By.XPATH, '//*[@id="popop"]/div/div[1]/div/span/span').click()
# 点击你想爬取的城市
web.find_element(By.XPATH, '//*[@id="popop"]/div/div[2]/div[1]/div[2]/div/table/tbody[1]/tr/td[5]/em').click()
web.find_element(By.XPATH, '//*[@id="popop"]/div/div[3]/span').click()  # 点击确定
time.sleep(5)

# ---------------------------------------------------------------------------------------------------
web.find_element(By.XPATH, '/html/body/div[2]/div[3]/div/div[1]/div[2]/div[1]/a').click()  # 下一页(第二页)
time.sleep(3)
for i in range(26):  # 爬第几页就减二
    web.find_element(By.XPATH, '/html/body/div[2]/div[3]/div/div[1]/div[2]/div[1]/a[2]').click()  # 下一页(第三页)
    time.sleep(3)
# ---------------------------------------------------------------------------------------------------


alist = web.find_elements(By.CLASS_NAME, 'el')
n = 0
for i in alist[:]:

    data = []
    # print(i)
    element = i.find_element(By.TAG_NAME, "span")
    web.execute_script("arguments[0].click();", element)

    # web.find_element(By.TAG_NAME, 'span').click()
    # time.sleep(3)
    web.switch_to.window(web.window_handles[-1])  # 跳转到倒数第一个窗口
    time.sleep(3)
    # # 工资待遇  公司名称  职位名称  要求描述
    aa = web.find_element(By.XPATH, '/html/body/div[3]/div[2]/div[2]/div/div[1]/h1').text
    bb = web.find_element(By.XPATH, '/html/body/div[3]/div[2]/div[2]/div/div[1]/p[1]/a[1]').text
    cc = web.find_element(By.XPATH, '/html/body/div[3]/div[2]/div[2]/div/div[1]/p[2]').text
    dd = web.find_element(By.XPATH, '/html/body/div[3]/div[2]/div[2]/div/div[1]/strong').text
    ee = web.find_element(By.XPATH, '/html/body/div[3]/div[2]/div[2]/div/div[1]/h1').text
    ff = web.find_element(By.XPATH, '/html/body/div[3]/div[2]/div[3]/div[1]/div').text
    data.append(aa)
    data.append(bb)
    data.append(cc)
    data.append(dd)
    data.append(ee)
    data.append(ff)

    datalist.append(data)
    # print(datalist)  # 显示列表中的每一个元素

    web.close()  # 关闭窗口
    web.switch_to.window(web.window_handles[0])  # 调整窗口到最开始的那个页面
    time.sleep(4)
    n += 1
    print(n)
    # except Exception:
    #     break


book = xlwt.Workbook(encoding="utf-8", style_compression=0)  # 创建workbook对象
sheet = book.add_sheet('ssssss', cell_overwrite_ok=True)  # 创建工作表
col = ('post', "emolument", "firm", "content", "qualifications")
# 表头字段的写入
for i in range(0, len(col)):
    sheet.write(0, i, col[i])  # 列名
# 数据字段的写入
for i in range(0, len(datalist)):
    # print("第{}条".format(i + 1))
    data = datalist[i]
    for j in range(0, len(data)):
        sheet.write(i + 1, j, data[j])
book.save('student.xls')  # 保存数据表
print("save ok..")
